package com.bmw.scene1.service.impl;

import com.bmw.scene1.dao.OrderInfoBakDao;
import com.bmw.scene1.service.OrderInfoBakService;
import com.bmw.scene1.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Classname: OrderInfoBakServiceImpl
 * Package: com.bmw.scene1.service.impl
 * Description:
 *
 * @Author 余集楠
 * @Create 2024/6/21 0021 16:31
 * @Version 1.8
 */
@Service
@Slf4j
public class OrderInfoBakServiceImpl implements OrderInfoBakService {
    @Autowired
    OrderInfoBakDao orderInfoBakDao;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public Boolean transfer() {
        int bak = orderInfoBakDao.countBak();
        int beforePay = orderInfoBakDao.countTotal();
        log.info("===[支付前,order_info_bak表中的数据{}条]===", bak);
        log.info("===[支付前,order_info表中的数据{}条]===", beforePay);
//        if (beforePay > 0) {
//            log.error("===[order_info表中已有数据，本次操作直接返回失败]===");
//            return false;
//        }

        log.info("===[开始transfer()方法]===");
        orderInfoBakDao.transfer();
        int afterCount = orderInfoBakDao.countTotal();
        log.info("===[支付完成，支付后order_info表中的数据为{}条]===", afterCount);
        if (afterCount > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean transferExpand() {
        log.info("===[开始调用幂等性校验的支付接口]===");
        //生成唯一标识
        String token="transfer";
        if(!redisUtil.setIfAbsent(token,1)){
            log.error("===[重复执行API,直接返回失败]===");
            return false;
        }else{
            try{
                return transfer();
            }catch (Exception e){
                log.error("===[支付异常]===",e);
                return false;
            }finally {
                redisUtil.del(token);
            }
        }

    }
}
